// pages/order/order.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.navBarHeight + app.globalData.statusBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    orderList: [],
    orderFuncList: [
      {
        id: 0,
        name: "全部",
        selected: true
      },
      {
        id: 1,
        name: "待付款",
        selected: false
      },
      {
        id: 2,
        name: "待收货",
        selected: false
      },
      {
        id: 3,
        name: "已完成",
        selected: false
      },
      {
        id: 4,
        name: "已取消",
        selected: false
      }
    ],
    orderStatusList: [
      {
        id: 1,
        value: 1,
        name: "待付款"
      },
      {
        id: 2,
        value: 2,
        name: "待收货"
      },
      {
        id: 3,
        value: 3,
        name: "退换货"
      },
      {
        id: 4,
        value: 4,
        name: "已取消"
      }
    ],
    pageIndex: 1,
    pageSize: 20
  },

  /***
   * 去支付
   */
  toPay(){

  },

  /**
   * 联系商家
   */
  toAcceptGoods(){
    wx.makePhoneCall({
      phoneNumber: '19196163234',
      success:()=>{
        console.log("电话拨打成功...")
      },
      error: (()=>{
        console.log("电话拨打失败...")
      })
    })
  },

  changeOrderStatus( event ){
    let index = event.currentTarget.dataset.index;
    if( !index ){
      index = 0;
    }
    console.log("index: ",index);
    let orderFuncList = this.data.orderFuncList
    orderFuncList = orderFuncList.map( item=>{
      return {
        ...item,
        selected: false
      }
    });
    
    let orderFunItem = orderFuncList[index];
    if( orderFunItem ){
      orderFunItem.selected = true;
    }
    this.setData({
      orderFuncList,
      pageIndex: 1
    });
    this.getOrderList( 1, orderFunItem.id );
  },

  toEvalute( event ){

  },

  toBuyAgain( event ){

  },

  getOrderList(  pageIndex,  orderStatus=0 ){
      wx.request({
        url: 'http://localhost:8082/app/omsOrderInfo/pagination',
        method: "GET",
        data:{
          pageIndex,
          pageSize: this.data.pageSize,
          orderStatus
        },
        success: (res) => {
          res = res.data;
          if( res.code != 200 ){
            return;
          }
          let data = res.data;
          let list =  data.list;
          list = list.map( item=>{
            let goodsAmountPrice = item.goodsAmountPrice +"";
            let priceInt = goodsAmountPrice.substring(0, goodsAmountPrice.length-2)
            let priceFloat =  goodsAmountPrice.substring(goodsAmountPrice.length-2)
            return {
              ...item,
              goodsAmountPriceStr: `${priceInt}.${priceFloat}`
            }
          } )
          console.log(list)
          this.setData({
            orderList: list
          })
        },
        fail: (err)=>{
          console.error("2222", err)
        }
      })
  },

  toBack(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("options: ", options)
    let orderStatus = options.orderStatus;
    this.setData({
      orderStatus: Number(orderStatus)
    })
    let event = {
      currentTarget: {
        dataset: {
          index: Number(orderStatus)
        }
      }
    }
    this.changeOrderStatus(event)
  },

  toOrderDetailPage( event ){
    let orderId = event.currentTarget.dataset.orderId;
    wx.navigateTo({
      url: `/pages/order-detail/order-detail?orderId=${orderId}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})